package com.example.gybicycle.controller.web;
import com.example.gybicycle.entity.SysMenuData;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.example.gybicycle.common.Log.SysLoging;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.util.*;
import com.example.gybicycle.entity.SysMenu;
import com.example.gybicycle.entity.LoginInfo;
import com.example.gybicycle.service.SysMenuService;



@Tag(name = "菜单")
@RestController
@RequestMapping("/web/sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @PostMapping("/get")
    @SysLoging("菜单管理-查看")
    @Operation(summary = "根据id查看")
    @RequiresPermissions("menu:list")
    @ApiOperationSupport(includeParameters = {"sysMenu.id"})
    public Result get(@RequestBody SysMenu sysMenu){
      sysMenu=sysMenuService.get(sysMenu.getId());
        return Result.success(sysMenu);
    }
    @PostMapping("/list")
    @Operation(summary =  "列表查看")
    @ApiOperationSupport(includeParameters = {"page.id","page.page","page.size","page.name","sysMenu.state"})
    @RequiresPermissions("menu:list")
    @SysLoging("菜单管理-查看")
    public Result findList(@RequestBody(required=false) SysMenu page){
//
//        Page<SysMenu> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
//                () ->);
        List<SysMenu> list=sysMenuService.findList(page);
        List<SysMenuData> tree=TreeUtil.menuToTree(list);
        return Result.success(tree);
    }
    @PostMapping("/insert")
    @Operation(summary =  "添加")
    @RequiresPermissions("menu:insert")
    @SysLoging("菜单管理-添加")
    public Result insert(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
        LoginInfo loginInfo = LoginUtil.getLoginInfo(request);
        if (loginInfo == null)
            return Result.loginOut();
        if(sysMenu.getRedirect()!=null&&sysMenu.getRedirect().equals("")){
            sysMenu.setRedirect(null);
        }
        if(sysMenu.getPath()==null)
            sysMenu.setPath("");

        int num = sysMenuService.insert(sysMenu);
        return Result.reData(num, sysMenu);
    }
    @PostMapping("/update")
    @Operation(summary =  "修改")
    @RequiresPermissions("menu:update")
    @SysLoging("菜单管理-修改")
    public Result update(@RequestBody SysMenu sysMenu, HttpServletRequest request){
        LoginInfo loginInfo=LoginUtil.getLoginInfo(request);
        if(loginInfo==null)
            return Result.loginOut();
        int num=sysMenuService.update(sysMenu);
        return Result.reData(num,sysMenu);
    }
  @PostMapping("/delete")
    @Operation(summary =  "删除")
    @RequiresPermissions("menu:delete")
    @SysLoging("菜单管理-删除")
    public Result delete(@RequestBody SysMenu entity){
        int num=sysMenuService.delete(entity.getId());
        if(num>0)
            return Result.success("删除成功");
        else 
            return Result.fail("删除失败");
    }
     @PostMapping("deletelist")
    @Operation(summary =  "批量删除")
    @ApiOperationSupport(includeParameters = {"sysMenu.id"})
    @RequiresPermissions("menu:delete")
    @SysLoging("菜单管理-批量删除")
    public Result deletelist(@RequestBody List<String> data){
        int num=sysMenuService.deleteByBatch(data);
         if(num>0)
             return Result.success("删除成功");
         else
             return Result.fail("删除失败"); 
    }
}